<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-23 10:02:17
 * @Description: 用户的保险充值 记录
 */
class AcountInsurance extends Eloquent
{
    protected $table = "acount_insurance_record";

    /**
     * @Author:      lyt
     * @DateTime:  2017-05-23 10:56:49
     * @Description: 用于保险单的 充值
     */
    public static function rechange($in_id,$user_id,$sc_user,$id_card,$amount){
        try{
            $response = DB::transaction(function() use ($in_id,$user_id,$sc_user,$id_card,$amount){
                //查保险记录
                $is=Insurance::where("id",$in_id)->lockForUpdate()->first();
                //检查是否有充值记录
                $ai=AcountInsurance::find($in_id);
                if($ai){
                    DB::rollback();
                    return Lang::get("scs_error.insurance_rechange_exits_90017");
                
                }
                //交易流水         

                $arr_id=Account::service_recharge($user_id,13,2,$amount,'sc',$sc_user->id,$sc_user->shop_id);
                //记录保险充值记录
                $account_no = Account::get_account_details_no($user_id,$sc_user->shop_id,2);
                $bbr=new AcountInsurance();
                $bbr->id=$in_id;
                $bbr->user_id=$user_id;
                $bbr->account_no=$account_no;
                $bbr->aurr_id=$arr_id;
                $bbr->amount=$amount;
                $bbr->created_by=$sc_user->id;
                $bbr->implement_time=Common::currentTimeString();               
                $bbr->save();                
                AcountUserRecharge::where("id",$arr_id)->update(array("pay_id"=>$in_id));
                return array('code' => 0, 'msg' => 'ok', 'result' => array("arr_id"=>$arr_id));
            });
            return $response;
        } catch (Exception $e) {
            Log::error("error", array("system" => "Account" ,"event" => "rechange" ,"exception" => $e));
            return false;
        }
    }

}